/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web.user;

import java.io.IOException;
import org.fyx.web.corejsf.EmailValidator;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.entity.SecureUser;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.secure.ejb.SecureRoles;
import org.fyx.util.FyxUtility;
import org.fyx.web.context.WebContext;
import org.primefaces.component.message.Message;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class SecureUserAdd {

    @EJB
    private SecureLocal secure;
    @EJB
    private MulaLocal mula;
    private SecureUser user = new SecureUser();
    private String emailStamp = "";
    private String passwordStamp = "";
    private boolean skip;
    private static Logger logger = Logger.getLogger(SecureUserAdd.class.getName());

    @PostConstruct
    public void postConstruct(){
        FyxUtility.checkRole(SecureRoles.USER_MANAGER);
    }
    
    public SecureUserAdd() {
        System.out.println("UserWizard vytvoren");
    }

    public SecureUser getUser() {
        return user;
    }

    public void setUser(SecureUser user) {
        this.user = user;
    }

    public String getEmailStamp() {
        return emailStamp;
    }

    public void setEmailStamp(String emailStamp) {
        this.emailStamp = emailStamp;
    }

    public String getPasswordStamp() {
        return passwordStamp;
    }

    public void setPasswordStamp(String passwordStamp) {
        this.passwordStamp = passwordStamp;
    }

    public String getEmailMsg() {
        if (user.getEmail() == null) {
            return "";
        }
        if (secure.emailExists(user.getEmail())) {
            return mula.get("This email is allredy registred.");
        }
        if (!EmailValidator.validate(user.getEmail())) {
            return mula.get("Email is not in correct format.");
        }
        return "";
    }

    public String getEmailStampMsg() {
        if (!isEmailStampValid() && !emailStamp.isEmpty()) {
            addError(null);
            return mula.get("Email is not same.");
        }
        return "";
    }

    public boolean isEmailStampValid() {
        return emailStamp.equals(user.getEmail());
    }

    public void save(ActionEvent actionEvent) throws IOException {
        secure.addUser(user);
        FacesMessage msg = new FacesMessage(mula.get("User was created."),
                mula.get("Welcome") + " " + user.getFirstname());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        FacesContext.getCurrentInstance().getExternalContext().
                redirect(WebContext.getCurrentContext().getBaseUrl());
    }

    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    public String onFlowProcess(FlowEvent event) {
        if(!isEmailStampValid() || 
            secure.emailExists(user.getEmail()) ||
           !EmailValidator.validate(user.getEmail())){
            return event.getOldStep();
        }
        
        if (skip) {
            skip = false;	//reset in case user goes back
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }

    public void addError(ActionEvent actionEvent) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR,
                mula.get("Email is not same."), mula.get("Please check your email.")));
    }

    public void onEmailActive() {
        Message mes =
                (Message) FacesContext.getCurrentInstance().getAttributes().get("email");
        mes.setDisplay("Ahoj");
        FacesMessage msg = new FacesMessage(
                "Sample error message", "PrimeFaces makes no mistakes");
        FacesContext.getCurrentInstance().addMessage("msgs", msg);
    }
}
